package com.letwal.cmcvnd.components.bar

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.util.TypedValue
import android.view.View
import android.widget.RelativeLayout
import androidx.core.content.ContextCompat
import androidx.fragment.app.FragmentManager
import com.letwal.cmcvnd.R
import com.letwal.cmcvnd.databinding.ViewBottomTabBarItemBinding

/**
<declare-styleable name="BottomTabBarItem">
<attr name="cwbtbiUncheckTextColor" format="color" />
<attr name="cwbtbiDidcheckTextColor" format="color" />
<attr name="cwbtbiUncheckImgRes" format="reference" />
<attr name="cwbtbiDidcheckImgRes" format="reference" />
<attr name="cwbtbiText" format="string" />
<attr name="cwbtbiTextSize" format="dimension" />
<attr name="cwbtbiDidChecked" format="boolean" />
</declare-styleable>
 * */
class BottomTabBarItem : RelativeLayout {

    lateinit var mViewBinding: ViewBottomTabBarItemBinding
    private lateinit var vRootView: View

    private var cwbtbiUncheckTextColor: Int = 0
    private var cwbtbiDidcheckTextColor: Int = 0
    private var cwbtbiUncheckImgRes: Int = 0
    private var cwbtbiDidcheckImgRes: Int = 0
    private var cwbtbiText: String? = null
    private var cwbtbiTextSize: Float = 0F
    private var cwbtbiDidChecked: Boolean = true

    private var mSupportFragmentManager: FragmentManager? = null

    private var mOnActionClickListener: (Int) -> Unit = {}

    constructor(context: Context?) : this(context, null)
    constructor(context: Context?, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : this(
        context,
        attrs,
        defStyleAttr,
        0
    )

    @SuppressLint("Recycle")
    constructor(
        context: Context?,
        attrs: AttributeSet?,
        defStyleAttr: Int,
        defStyleRes: Int,
    ) : super(
        context,
        attrs,
        defStyleAttr,
        defStyleRes
    ) {
        attrs?.let {
            context?.let { cxt ->
                val array = cxt.obtainStyledAttributes(attrs, R.styleable.BottomTabBarItem)
                cwbtbiUncheckTextColor = array.getColor(
                    R.styleable.BottomTabBarItem_cwbtbiUncheckTextColor,
                    ContextCompat.getColor(cxt, R.color.white)
                )

                cwbtbiDidcheckTextColor = array.getColor(
                    R.styleable.BottomTabBarItem_cwbtbiDidcheckTextColor,
                    ContextCompat.getColor(cxt, R.color.white)
                )

                cwbtbiTextSize = array.getDimension(
                    R.styleable.BottomTabBarItem_cwbtbiTextSize,
                    resources.getDimensionPixelSize(
                        R.dimen.sp_12
                    ).toFloat()
                )

                cwbtbiText =
                    array.getString(R.styleable.BottomTabBarItem_cwbtbiText)

                cwbtbiUncheckImgRes =
                    array.getResourceId(R.styleable.BottomTabBarItem_cwbtbiUncheckImgRes, 0)

                cwbtbiDidcheckImgRes =
                    array.getResourceId(
                        R.styleable.BottomTabBarItem_cwbtbiDidcheckImgRes,
                        0
                    )
                cwbtbiDidChecked = array.getBoolean(
                    R.styleable.BottomTabBarItem_cwbtbiDidChecked,
                    false
                )

                array.recycle()
            }
        }
        context?.let {
            vRootView =
                View.inflate(it, R.layout.view_bottom_tab_bar_item, this)
            mViewBinding =
                ViewBottomTabBarItemBinding.bind(vRootView)
            initSubviews()
        }
    }

    private fun initSubviews() {
        try {
            mViewBinding.textTabBarItem.text = cwbtbiText
            mViewBinding.textTabBarItem.setTextSize(
                TypedValue.COMPLEX_UNIT_PX,
                cwbtbiTextSize
            )

            mViewBinding.imageTabBarItemSelect.setImageResource(
                cwbtbiDidcheckImgRes
            )
            mViewBinding.imageTabBarItemUnSelect.setImageResource(
                cwbtbiUncheckImgRes
            )

            setCheck(cwbtbiDidChecked)

            mViewBinding.llTabbarItemSelectBg.setOnClickListener {
                mOnActionClickListener.invoke(1)
            }

            mViewBinding.imageTabBarItemSelect.setOnClickListener {
                mOnActionClickListener.invoke(1)
            }

            mViewBinding.imageTabBarItemUnSelect.setOnClickListener {
                mOnActionClickListener.invoke(1)
            }

        } catch (e: java.lang.Exception) {
            e.printStackTrace()
        }
    }

    fun setCheck(check: Boolean) {
        cwbtbiDidChecked = check
        if (cwbtbiDidChecked) {
            mViewBinding.vTabBarItemTopLine.setBackgroundColor(
                ContextCompat.getColor(
                    context,
                    R.color.main_red
                )
            )

            mViewBinding.imageTabBarItemSelect.setImageResource(
                cwbtbiDidcheckImgRes
            )

            mViewBinding.textTabBarItem.setTextColor(cwbtbiDidcheckTextColor)

        } else {
            mViewBinding.vTabBarItemTopLine.setBackgroundColor(
                ContextCompat.getColor(
                    context,
                    R.color.white
                )
            )

            mViewBinding.imageTabBarItemSelect.setImageResource(
                cwbtbiUncheckImgRes
            )

            mViewBinding.textTabBarItem.setTextColor(cwbtbiUncheckTextColor)
        }
    }

    fun setFragmentManager(manager: FragmentManager) {
        mSupportFragmentManager = manager
    }

    fun setOnActionClickListener(listener: (Int) -> Unit) {
        mOnActionClickListener = listener
    }
}